#pragma once 
#include "IResource.h"
#include <allegro5\allegro_audio.h>

namespace DarkStorm2D
{
namespace SoundCore
{
	class CSound : public IResource
	{
	protected: 
		ALLEGRO_SAMPLE* m_Sound;
		ALLEGRO_SAMPLE_ID m_SoundID;
	public:
		enum EPlayMode {
			ONCE = ALLEGRO_PLAYMODE_ONCE,
			LOOP = ALLEGRO_PLAYMODE_LOOP,
			BIDIR = ALLEGRO_PLAYMODE_BIDIR
		};
		enum EPanMode {
			NONE = -1000,
			CENTER = 0,
			LEFT = -1,
			RIGHT = 1,
		};

		CSound()
		{
			m_Type = IResource::RES_TYPE_SOUND;
			m_Sound = 0;
			memset(&m_SoundID,0,sizeof(m_SoundID));
		}
		~CSound()
		{
			Stop();
			if (m_Sound != 0)
				al_destroy_sample(m_Sound);
		}

		const CError& LoadFromFile(const std::string & Filename)
		{
			if (m_Sound == 0)
				m_Sound = al_load_sample(Filename.c_str());
			if (m_Sound != 0)
			{
				SetFilename(Filename);
				return CError();
			}
			return CError(CError::TYPE_SYSTEM_ERROR,CError::FILE_LOAD);
		}
		const CError& Reload()
		{
			al_destroy_sample(m_Sound);
			return LoadFromFile(m_Filename);
		}

		bool Play(float Gain, float Pan, float Speed = 1.0, int Loop = ONCE)
		{
			return al_play_sample(m_Sound,Gain,Pan,Speed,Loop,&m_SoundID);
		}

		void Stop()
		{
			al_stop_sample(&m_SoundID);
		}
	};
};
};